#include <QResizeEvent>
#include <QtCore/QDebug>

#include "formeditorw.h"
#include "widgetboxw.h"

#include "resourceeditorw.h"

ResourceEditorW *ResourceEditorW::m_self = 0;

ResourceEditorW::ResourceEditorW(QWidget *parent) : QWidget(parent), m_editor(0), m_initialized(false)
{
    if (m_self != 0)
        delete m_self;

    m_self = this;
    FormEditorW::instance()->setResourceEditor(this);
}

ResourceEditorW::~ResourceEditorW()
{
    m_self = 0;
    FormEditorW::instance()->setResourceEditor(0);

    if (m_editor != 0)
    {
        m_editor->hide();
        m_editor->setParent(0);
    }
}

void ResourceEditorW::initialize()
{
    if (!m_initialized)
    {
        m_initialized = true;
        FormEditorW::instance()->initialize();

        m_editor = QDesignerComponents::createResourceEditor(FormEditorW::instance()->formEditor(), this);
        FormEditorW::instance()->setResourceEditor(this);
    }
}

void ResourceEditorW::updateCustomWidgetLocation(const QString &path)
{
    WidgetBoxW *w = WidgetBoxW::instance();

    if (w != 0)
    {
        w->updateCustomWidgetLocation(path);
    }
}

ResourceEditorW *ResourceEditorW::instance()
{
    if (m_self == 0)
        m_self = new ResourceEditorW();

    m_self->initialize();

    return m_self;
}

/**
 * 更新资源。只在Qt Jambi安装后被调用
 */
void ResourceEditorW::updateResources(const QString &paths)
{
    if (m_editor != 0)
        QMetaObject::invokeMethod(m_editor, "updateRootDirs", Q_ARG(QString, paths));
}

bool ResourceEditorW::initializeJambiPlugins(const QString &jambiBase, const QString &jambiPluginPath, const QString &customWidgetClassPath, const QString &resourcePath, const QString &jvm)
{
    return FormEditorW::instance()->initializeJambiPlugins(jambiBase, jambiPluginPath, customWidgetClassPath, resourcePath, jvm);
}


void ResourceEditorW::resizeEvent(QResizeEvent *event)
{
    if (m_editor != 0)
        m_editor->resize(event->size());

    QWidget::resizeEvent(event);
}

QSize ResourceEditorW::minimumSize()
{
    if (m_editor != 0)
        return m_editor->minimumSize();
    else
        return QWidget::minimumSize();
}

QString ResourceEditorW::pluginFailureString() const
{
    return FormEditorW::instance()->pluginFailureString();
}

